import 'package:flutter/material.dart';
import 'package:ichinesereader/controller/cache.dart';
import 'package:ichinesereader/controller/user.dart';
import 'package:get/get.dart';
import 'package:ichinesereader/models/teacher/TeacherClass.dart';
import 'package:hive_flutter/hive_flutter.dart';

class ClassSelectionDropDown extends StatelessWidget {
  ClassSelectionDropDown({
    Key? key,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    UserController _usercontroller = Get.find();

    // if (_usercontroller.currentClass.value.classId == null)
    //   _usercontroller.fetchMyClasses();

    return Obx(() => DropdownButton(
          icon: Icon(
            Icons.arrow_drop_down_outlined,
            color: Theme.of(context).primaryColorDark,
          ),
          hint: Text('Choose Class'.tr),
          value: _usercontroller.getCurrentClass(),
          items: _usercontroller.myClasses.map((selectedClass) {
            return DropdownMenuItem(
                value: selectedClass,
                child: Text(
                  selectedClass.className.toString(),
                  style: Theme.of(context).textTheme.titleSmall,
                  overflow: TextOverflow.ellipsis,
                ));
          }).toList(),
          onChanged: (value) async {
            _usercontroller.setCurrentStudentClass(value as TeacherClass);
            var box = Hive.box('appconfig');
            await box.put(
                CacheManagerKey['currentclass'].toString(), value.classId);
          },
        ));
  }
}
